//
//  WDDImageScrollView.m
//  SZBank
//
//  Created by rimi on 16/7/18.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "WDDImageScrollView.h"
#import "LocalDataManager.h"
@interface WDDImageScrollView()<UIScrollViewDelegate>
{
    NSMutableArray *imageArray;
    NSInteger _imageIndex;
    UILabel *pageCount;
}

@property (nonatomic,strong) UIImageView *bgImage;

//分页控制器
@property (nonatomic,strong) UIPageControl *pageControl;
//滚动视图
@property (nonatomic,strong) UIScrollView *scrollView;
//视图数组
@property (nonatomic,strong) NSMutableArray *imageViews;

@end
@implementation WDDImageScrollView

{
    CGFloat frameWidth;/**  自身宽度 */
    CGFloat frameHeight;/**  自身高度 */
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        frameWidth = self.frame.size.width;
        frameHeight = self.frame.size.height;
        imageArray = [NSMutableArray array];
        self.imageViews = [NSMutableArray array];
        /**  返回按钮 */
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(frameWidth * 0.03, frameHeight * 0.03, 80, 40)];
        [button setTitle:@"返回" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        button.backgroundColor = [UIColor clearColor];
        [button addTarget:self action:@selector(action_back) forControlEvents:UIControlEventTouchUpInside];
    
        [self addSubview:self.bgImage];
        [self addSubview:button];
    }
    return self;
}

- (void)action_back{
    
    
    
    [UIView animateWithDuration:0.8 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0.9 options:UIViewAnimationOptionTransitionNone animations:^{
        self.transform = CGAffineTransformMakeTranslation(0, frameHeight);
    } completion:^(BOOL finished) {
        NSArray *viewArray = @[self.scrollView,self.pageControl,pageCount];
        for (int i = 0; i < viewArray.count; i ++) {
            [viewArray[i] removeFromSuperview];
        }
        [self removeFromSuperview];
        self.transform = CGAffineTransformIdentity;
        
    }];
}

- (void)initUserInterface{
    [imageArray removeAllObjects];
    for (int i = 0; i < self.data.count; i ++) {
        UIImage *photo = [LocalDataManager getImageWithURL:self.data[i]];
        [imageArray addObject:photo];
    }
    /**  滚动视图 */
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(frameWidth * 0.05, frameHeight * 0.1, KWIDTH * 0.9, KHEIGHT * 0.8)];
    self.scrollView.backgroundColor = [UIColor clearColor];
    self.scrollView.contentSize = CGSizeMake(0.9 * KWIDTH * imageArray.count, KHEIGHT * 0.8);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    [self addSubview:self.scrollView];
    /**  分页控制器 */
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(frameWidth * 0.45, frameHeight * 0.94, 150, 40)];
    self.pageControl.numberOfPages = imageArray.count;
    self.pageControl.currentPage = 0;
    self.pageControl.pageIndicatorTintColor = [UIColor greenColor];
    self.pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    [self addSubview:self.pageControl];
        for (int j = 0; j < imageArray.count; j ++) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(j * frameWidth * 0.9, 0, KWIDTH * 0.9, KHEIGHT * 0.8)];
            imageView.contentMode = UIViewContentModeScaleAspectFit;
            imageView.image = imageArray[j];
            [self.scrollView addSubview:imageView];
        }
    /**  翻页计数 */
    NSString *pagesStr = [NSString stringWithFormat:@"%d/%ld",1,imageArray.count];
    pageCount = [[UILabel alloc] initWithFrame:CGRectMake(frameWidth * 0.47, frameHeight * 0.03, frameWidth * 0.05, frameHeight * 0.03)];
    pageCount.textAlignment = NSTextAlignmentCenter;
    pageCount.backgroundColor = [UIColor clearColor];
    pageCount.text = pagesStr;
    [self addSubview:pageCount];
}

#pragma mark -- UIScrollViewAccessibilityDelegate代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    int page = self.scrollView.contentOffset.x / (int)(KWIDTH * 0.9);
    self.pageControl.currentPage = page;
    NSString *pagesStr = [NSString stringWithFormat:@"%d/%ld",page + 1,imageArray.count];
    pageCount.text = pagesStr;
}

- (UIImageView *)bgImage{
    if (!_bgImage) {
        _bgImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frameWidth, frameHeight)];
        _bgImage.image = [LocalDataManager getImageWithSourceOfPath:@"小背景"];
    }
    return _bgImage;
}


@end
